//
//  HomeIngredientsViewController.m
//  Food
//
//  Created by dllo on 15/7/21.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "HomeIngredientsViewController.h"
#import "BaseHeader.h"
#import "IngredientsModel.h"
#import "IngredientsCollectionViewCell.h"
#import "AFNetworkHandler.h"
#import "IngredientsFoodController.h"
@interface HomeIngredientsViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, retain) UISegmentedControl *seg;
@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain) UIScrollView *scrollView;

@end

@implementation HomeIngredientsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
 
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"naviBack"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    
    [self createScrollView];
    [self createCollectionView];
    [self getData];
}

- (void) back
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void) getData
{
    UIActivityIndicatorView *activityIndicator = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge] autorelease];
    
    activityIndicator.frame = CGRectMake(0, 0, 100, 100);
    
    activityIndicator.color = [UIColor grayColor];
    
    activityIndicator.center = CGPointMake(ScreenWidth / 2.0, ScreenHeight / 2.0 - 50);
    
    [self.view addSubview:activityIndicator];
    
    [activityIndicator startAnimating];
    NSString *urlStr = [NSString stringWithFormat:@"http://121.41.88.194:80/HandheldKitchen/api/more/tblmmaterialtype!getMaterialList.do?is_traditional=0&page=1&pageRecord=1000&materialTypeId=%@", [self.arr[self.seg.selectedSegmentIndex] objectForKey:@"id"]];
    [AFNetworkHandler getWithAFNByURL:urlStr completion:^(id result) {
        
        NSArray *arr = [result objectForKey:@"data"];
        self.ingredientsArr = [IngredientsModel arrayWithModelByArray:arr];
        [self.collectionView reloadData];
        [activityIndicator stopAnimating];
    }];

}

- (void) createScrollView
{
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 35)];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.scrollView.backgroundColor = [UIColor whiteColor];
    
    self.scrollView.showsHorizontalScrollIndicator = NO;
    
    self.scrollView.contentSize = CGSizeMake(80 * self.arr.count, 35);
    
    [self.view addSubview:self.scrollView];
    NSMutableArray *segArr =  [NSMutableArray array];
    for (NSDictionary *dic in self.arr) {
        NSString *name = dic[@"name"];
        [segArr addObject:name];
    }
    self.seg = [[UISegmentedControl alloc] initWithItems:segArr];
    
    self.seg.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"backGround.jpg"]];
    
    self.seg.frame = CGRectMake(0, 0, 80 * self.arr.count, 35);
    
    self.seg.tintColor = [UIColor grayColor];
    
    self.seg.selectedSegmentIndex = 0;
    
    [self.seg addTarget:self action:@selector(fun) forControlEvents:UIControlEventValueChanged];
    [self.scrollView addSubview:self.seg];
    
}


- (void) fun
{
    self.navigationItem.title = [self.arr[self.seg.selectedSegmentIndex] objectForKey:@"name"];
    [self getData];
}


- (void) createCollectionView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    layout.itemSize = CGSizeMake((ScreenWidth - 50) / 2, 50);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(10, 40, ScreenWidth - 20, ScreenHeight - 64 - 49 - 40) collectionViewLayout:layout];
    
    self.collectionView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    self.collectionView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"backGround"]];
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    self.collectionView.showsVerticalScrollIndicator = NO;
    
    [self.collectionView registerClass:[IngredientsCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    
    [self.view addSubview:self.collectionView];
    
    
    
}

- (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.ingredientsArr.count;
}

- (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    IngredientsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    
    cell.model = self.ingredientsArr[indexPath.item];
    
    return cell;
}

#pragma mark - cell 被选中
- (void) collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    IngredientsFoodController *ingredients = [[IngredientsFoodController alloc] init];
    ingredients.title = [self.ingredientsArr[indexPath.item] name];
    ingredients.getID = [self.ingredientsArr[indexPath.item] materialId];
    [self.navigationController pushViewController:ingredients animated:YES];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
